package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import java.util.Objects;


public class MainActivityWeb extends AppCompatActivity {

    private AppCompatEditText urlEditText;
    private WebView mainwebview;
    private AppCompatTextView  refrTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置布局文件（绑定布局文件）
        setContentView(R.layout.activityy_webview);
        urlEditText = findViewById(R.id.urlEditText);
        mainwebview = findViewById(R.id.mainWebView);
        refrTextView = findViewById(R.id.refrTextView);


        //接收父Activity传递的数据
        String url = getIntent().getStringExtra("url");

        //设置webview
        WebSettings webSettings = mainwebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //缓存设置
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

        mainwebview.loadUrl(url);

        mainwebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                urlEditText.setText(url);
                mainwebview.loadUrl(url);
                return false;
            }
        });
        refrTextView.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("url",urlEditText.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
        });
        refrTextView.setOnClickListener(view -> {
            mainwebview.loadUrl(Objects.requireNonNull(urlEditText.getText()).toString());
        });

        //本周的作业 自行实现 后退 前进 刷新

        Log.d("TAG", "WebActivity:onCreate");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("TAG", "WebActivity:onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("TAG", "WebActivity:onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("TAG", "WebActivity:onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("TAG", "WebActivity:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "WebActivity:onDestory");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("TAG", "WebActivity:onRestart");
    }

}

